package ejb.updater;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;

import org.jboss.ejb3.annotation.ResourceAdapter;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
@ResourceAdapter("quartz-ra.rar")
@MessageDriven(activationConfig = {
		@ActivationConfigProperty(propertyName="cronTrigger", propertyValue="0 0 0-23/12 * * ?")	
	}, messageListenerInterface=Job.class)
public class ForecastedWeatherUpdater extends SchedWeatherUpdater {
	//"0 0 0-23/12 * * ?"
	private int i = 0;
	
	public ForecastedWeatherUpdater() {
		super();
	}

	@Override
	public void execute(JobExecutionContext arg0) {
		AltCityForecast[] cfarr = forecasts[i%(FORECASTED_DAYS)];
		for(AltCityForecast cf : cfarr) {
			System.out.println("Forecast check...");
			XMLParserV2 parser = null;
			try {
				parser = new XMLParserV2(new URL(makeUrl(cf.getName())).openStream());
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int conds = parser.getForecastConditions(cf.getPlusday());
			System.out.println("+day conds: " + conds);
			int[] temparr = parser.getForecastTemperature(cf.getPlusday());
			int temp = (int) ((temparr[0]+temparr[1])/2.0);
			System.out.println("+day temp: " + temp);
			if(conds != cf.getConditions() ||
			   temp  != cf.getTemperature()) {
				System.out.println("Update [F]: " + cf.getName() + "[" + temp +", "+ conds +"]");
				cf.setConditions(conds);
				cf.setTemperature(temp);
				sendMessage(false, cf, true, cf.getPlusday());
			}
		}
		i++;
	}
}
